package com.js.data.system.controller;


import com.js.data.common.api.CommonResult;
import com.js.data.system.annotation.Log;
import com.js.data.system.dto.AreaDTO;
import com.js.data.system.service.AreaService;
import com.js.data.system.vo.AreaTreeVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * AreaController-地区表 前端控制器
 *
 * @author wujj
 * @version 1.0.0
 * @since 2020/6/3 19:06
 */
@RestController
@RequestMapping("/sys")
public class AreaController {

    @Resource
    private AreaService areaService;

    /**
     * 地区树形
     *
     * @param parentId 地区父id,默认为0
     * @return List<AreaTreeVO>
     * @author wujj
     * @since 2020/6/4 19:16
     */
    @PreAuthorize("@perm.check('system:area:tree','system:dept:tree')")
    @GetMapping(value = "/area/{parentId}", produces = "application/json; charset=utf-8")
    public CommonResult<List<AreaTreeVO>> treeArea(@PathVariable Long parentId) {
        return CommonResult.success(areaService.treeArea(parentId));
    }

    /**
     * 新增地区
     *
     * @param areaDTO 地区新增表单
     * @return areaId
     * @author wujj
     * @since 2020/6/4 19:33
     */
    @PreAuthorize("@perm.check('system:area:add')")
    @Log(value = "新增地区")
    @PostMapping(value = "/area", produces = "application/json; charset=utf-8")
    public CommonResult<Void> saveArea(@Valid @RequestBody AreaDTO areaDTO) {
        areaService.saveArea(areaDTO);
        return CommonResult.success();
    }


    /**
     * 修改地区
     *
     * @param id      地区id
     * @param areaDTO 地区表单
     * @return void
     */
    @PreAuthorize("@perm.check('system:area:edit')")
    @Log(value = "修改地区")
    @PutMapping(value = "/area/{id}")
    public CommonResult<Void> editArea(@PathVariable Long id, @Validated @RequestBody AreaDTO areaDTO) {
        areaService.editArea(id, areaDTO);
        return CommonResult.success();
    }

    /**
     * 删除地区
     *
     * @param id 地区id
     * @return void
     */
    @PreAuthorize("@perm.check('system:area:del')")
    @Log(value = "删除地区")
    @DeleteMapping(value = "/area/{id}")
    public CommonResult<Void> deleteArea(@PathVariable Long id) {
        areaService.deleteArea(id);
        return CommonResult.success();
    }
}
